$(function () {

    var html_data = {
        baseConfig: baseConfig,
        sessionid: '',
        sessionid_q: '',
        selected_express_id: parseInt(GetQueryString('express_id')),
        selected_express_name: "",
        is_select_address: false,
        express_is_modefy: GetQueryString("express_is_modefy")=="true"?true:false,
        change_obj:{},
        province_ids: [],
        province_list: [],
        express_item_list: []

    };
    var app = new Vue({
        el: '#host_express_item_html',
        data: html_data,
        methods: {

            clear_select_address: function () {
                html_data.province_ids = [];
                var province_ids = [];
                _(html_data.province_ids).each(function (item) {
                    province_ids.push(parseInt(item));
                })
                _(html_data.province_list).each(function (item) {
                    if(province_ids.indexOf(item.id)>=0){
                        item.selected = true;
                    }else{
                        item.selected = false;
                    }
                });
                setTimeout(function () {
                    _(html_data.province_list).each(function (obj, index) {
                        $("#checkbox_"+index).prop("checked",obj.selected);
                    })
                }, 200);
            },

            finish_select_address: function () {
                html_data.is_select_address = false;
                html_data.province_ids = [];
                _(html_data.province_list).each(function (obj) {
                    if(obj.selected){
                        html_data.province_ids.push(obj.id);
                    }
                })
                html_data.selected_express_item.address_list_ids = html_data.province_ids;
                html_data.province_ids = [];
                app.change_item(html_data.selected_express_item);
                html_data.selected_express_item.is_change = true;
            },
            create_express_item: function () {
                api.lorder.create_express_template_item({express_id: html_data.selected_express_id}).then(function (result) {
                    if (result.success) {
                        app.refresh_data();
                    } else {
                        Magiclamp.mcAlert(result.message);
                    }
                })
            },
            modefy_express_item_address: function (express_item) {
                html_data.selected_express_item = express_item;
                html_data.province_ids = express_item.address_list_ids;
                var province_ids = [];
                _(html_data.province_ids).each(function (item) {
                    province_ids.push(parseInt(item));
                })
                _(html_data.province_list).each(function (item) {
                    if(province_ids.indexOf(item.id)>=0){
                        item.selected = true;
                    }else{
                        item.selected = false;
                    }
                });
                setTimeout(function () {
                    _(html_data.province_list).each(function (obj, index) {
                        $("#checkbox_"+index).prop("checked",obj.selected);
                    })
                }, 200);

                html_data.is_select_address = true;
            },
            modefy_express_item: function (express_item) {
                api.lorder.update_express_template_item({
                    express_id: html_data.selected_express_id,
                    express_item_id: express_item.id,
                    first_num: express_item.first_num,
                    more_num: express_item.more_num,
                    first_price: express_item.first_price,
                    more_price: express_item.more_price,
                    is_free: express_item.is_free,
                    free_num: express_item.free_num,
                    address_list: express_item.address_list_ids.join(","),
                }).then(function (result) {
                    if (result.success) {
                        app.refresh_data();
                        Magiclamp.mcAlert("提示", "数据保存成功");
                    }
                });
            },
            selected_add:function(index){
                var item = html_data.province_list[index];
                item.selected = !item.selected;
                _(html_data.province_list).each(function (obj, index) {
                    $("#checkbox_"+index).prop("checked",obj.selected);
                })
            },
            change_item: function (express_item) {
                express_item.is_change = false;
                if (express_item.first_num != express_item.old.first_num) {
                    express_item.is_change = true;
                }
                if (express_item.first_price != express_item.old.first_price) {
                    express_item.is_change = true;
                }
                if (express_item.more_num != express_item.old.more_num) {
                    express_item.is_change = true;
                }
                if (express_item.more_price != express_item.old.more_price) {
                    express_item.is_change = true;
                }
                if (express_item.is_free != express_item.old.is_free) {
                    express_item.is_change = true;
                }
                if (express_item.free_num != express_item.old.free_num) {
                    express_item.is_change = true;
                }

                var str = "";
                _(html_data.province_list).each(function (item) {
                    if (express_item.address_list_ids.indexOf(item.id.toString()) >= 0 || express_item.address_list_ids.indexOf(item.id) >= 0) {
                        str += item.name + "、";
                    }
                });
                if(express_item.old.address_list_str){
                    if (str != express_item.old.address_list_str) {
                        express_item.is_change = true;
                    }
                }else{
                    express_item.old.address_list_str = str;
                }

                express_item.address_list_str = str;
            },
            refresh_data:function () {
                api.lorder.query_express_template_item_list({express_id: parseInt(GetQueryString('express_id'))}).then(function (data) {
                    html_data.express_item_list = data.result.list;
                    html_data.selected_express_name = data.result.list[0]["express__name"];
                    _(html_data.express_item_list).each(function (item) {
                        if (item.address_list) {
                            item.address_list_ids = item.address_list.split(",");
                        } else {
                            item.address_list_ids = [];
                        }
                        item.is_change = false;
                        item.old = copy(item);
                        app.change_item(item);
                    })
                });
            },

            delete_express_item: function (express_item) {
                api.lorder.delete_express_template_item({
                    express_id: html_data.selected_express_id,
                    express_item_id: express_item.id
                }).then(function (data) {
                    express_item.is_active = false;
                })
            },
            page_back: function () {
                baseConfig.page_back();
            }
        }
    });

    html_data.sessionid = GetQueryString('sessionid');
    if (html_data.sessionid) {
        html_data.sessionid_q = "sessionid=" + html_data.sessionid;
        client_storage('sessionid', html_data.sessionid);
    }

    function copy(obj) {
        var newobj = {};
        for (var attr in obj) {
            newobj[attr] = obj[attr];
        }
        return newobj;
    }



    api.lorder.get_provinces_list().then(function (data) {
        html_data.province_list = data.result.list;

        _(html_data.province_list).each(function (item) {
            item.selected = false;
        })
        app.refresh_data();
    });



});
